import { reactive, onMounted } from 'vue'
import { banner, personalized, newestAlbum, firstMv, hotSinger } from '@/services/home'

export const homePersonalized = () => {
  // 首页轮播图
  const banner_info = reactive({
    banner_list: [],
    banner_loading: true
  })
  // 推荐歌单
  const playlist_info = reactive({
    playlist_list: [],
    playlist_total: 0,
    playlist_params: 12,
    playlist_loading: true
  })
  // 新碟上架
  const album_info = reactive({
    album_list: [],
    album_count: 0,
    album_loading: false
  })
  // 最新 MV
  const mv_info = reactive({
    mv_area: ['全部', '内地', '港台', '欧美', '日本', '韩国'],
    mv_list: [],
    mv_index: 0,
    mv_params: { limit: 10 },
    mv_count: 10,
    mv_loading: true
  })
  // 热门歌手
  const hot_singer = reactive({
    singer_list: [],
    singer_loading: true,
    singer_params: { limit: 28 }
  })

  // 获取首页轮播图
  const getBanner = async () => {
    banner_info.banner_loading = true
    const { banners } = await banner()
    banner_info.banner_list = banners
    banner_info.banner_loading = false
  }
  // 获取推荐歌单
  const getPersonalizeds = async (params) => {
    playlist_info.playlist_loading = true
    const res = await personalized(params)
    playlist_info.playlist_loading = false
    playlist_info.playlist_list = res.result
  }
  // 获取新碟上架
  const getNewestAlbumlist = async () => {
    album_info.album_loading = true
    const res = await newestAlbum()
    album_info.album_list = res.albums
    album_info.album_count = res.albums.length
    album_info.album_loading = false
  }
  // 获取最新 MV
  const getMv = async (params) => {
    const res = await firstMv(params)
    mv_info.mv_list = res.data
    mv_info.mv_loading = false
  }
  // 获取热门歌手
  const getHotSinger = async (params) => {
    hot_singer.singer_loading = true
    const res = await hotSinger(params)
    hot_singer.singer_list = res.artists
    hot_singer.singer_loading = false
  }
  onMounted(() => {
    Promise.all([
      getBanner(),
      getPersonalizeds(playlist_info.playlist_params),
      getNewestAlbumlist(),
      getMv(mv_info.mv_params),
      getHotSinger(hot_singer.singer_params)
    ])
  })
  return {
    banner_info,
    playlist_info,
    album_info,
    mv_info,
    hot_singer
  }
}
